class Friend:
    def __init__(self):
        self.friend_li=[]
    def welcome(self):
        print("**欢迎使用好友管理系统**")
        print("1:添加好友")
        print("2:删除好友")
        print("3:备注好友")
        print("4:展示好友")
        print("5:好友分组")
        print("6:退出")
        while True:
            option = input("请选择功能\n")
            if option =='1':
                self.add_friend()
            elif option =='2':
                self.del_friend()
            elif option =='3':
                self.modify_friend()
            elif option=='4':
                self.show_friend()
            elif option=='5':
                self.group_friend()
            elif option =='6':
                break

    def add_friend(self):
        add_friend = input("请输入要添加的好友")
        self.friend_li.append(add_friend)
        print('好友添加成功')

    def get_all_friends(self):
        new_li =[]
        for friend_li_elem in self.friend_li:
            if type(friend_li_elem) == dict:
                [new_li.append(dict_elem_name)for 
                dict_elem in friend_li_elem.values()
                for dict_elem_name in dict_elem] 
            else:
                new_li.append(friend_li_elem)
        return new_li

    def get_all_groups(self):
        groups=[]
        for friend_li_elem in self.friend_li:
            if type(friend_li_elem) ==dict:
                groups.append(friend_li_elem)
        return groups

    def get_all_groups_name(self):
        groups_name=[]
        for dict_elem in self.get_all_friends():
            for j in dict_elem:
                groups_name.append(j)
        return groups_name

    def del_friend(self):
        if len(self.friend_li) !=0:
            del_name = input("请输入删除好友姓名：")
            if del_name in self.friend_1i:
                self.friend_li.remove(del_name)
                print('删除成功')
            else:
                if del_name in self.get_all_friends(): 
                    for group_data in self.get_all_groups(): 
                        for group_friend_li in group_data.values():
                            if del_name in group_friend_li:group_friend_li.remove(del_name)
                            continue
                            print('删除成功')
        else:
            print('好友列表为空')
    
    def modify_friend(self):
        friends = self.get_all_friends()
        if len(friends)==0:
            print('好友列表为空')
        else:
            before_name = input("请输入要修改的好友姓名：")
            after_name=input("请输入修改后的好友姓名：")
            if before_name in self.friend_li:
                friend_index=self.friend_li.index(before_name)
                self.friend_li[friend_index]= after_name
                print("备注成功")
            elif before_name not in self.friend_li:
                for friend_li_elem in self.friend_li:
                    if type(friend_li_elem) == dict:
                        for dict_elem in friend_li_elem.values():
                            if before_name in dict_elem:
                                modify_index =dict_elem.index(before_name)
                                dict_elem[modify_index]=after_name
                print('备注成功')
    
    def group_friend(self):
        create_group = input("是否创建新的分组y/n\n")
        friends = self.get_all_friends()
        if create_group == 'y':
            if len(friends)==0:
                print("当前没有任何好友")
            else:
                group_name = input("请输入分组名称：\n")
                group_name_li = list()
                print(friends)
                friend_name = input("请输入好友名称：\n")
                if friend_name in friends:
                    all_friend = []
                    for friend_li_elem in self.friend_li:
                        if type(friend_li_elem) == dict:
                            [all_friend.append(dict_friends) for
                             dict_elem in friend_li_elem.values()
                             for dict_friends in dict_elem]
                        else:
                            all_friend.append(friend_li_elem)
                    if friend_name in all_friend:
                        group_name_li.append(friend_name)
                        self.friend_li.remove(friend_name)
                        friend_dict = dict()
                        friend_dict[group_name] = group_name_li
                        self.friend_li.append(friend_dict)
                    else:
                        print("请输入正确的名称")
                else:
                    print('请输入正确好友名称')
        elif create_group =='n':
            current_groups = self.get_all_groups()
            print('当前分组:')
            for  current_group in current_groups:
                for group_name in current_group:
                    print(group_name)
            add_group = input('请选择添加的组:\n')
            if add_group in self.get_all_groups_name():
                add_name = input('请选择添加的好友名称：\n')
                if add_name in self.friend_li:
                    if add_name not in current_groups:
                        add_group_index = \
                            self.get_all_groups_name().index(add_group)
                        current_groups[add_group_index][
                            add_group].append(add_name)
                else:
                    print('该好友已在其他分组中')
            else:
                print('请输入正确的组名')

if __name__ == '__main__':
    friend = Friend()
    friend.welcome()





